<extend name="public/form"/>
<block name="css"></block>
<block name="content">
    <div class="layui-card-header "></div>
    <div class="layui-card-body ">
        <form id="form1" class="layui-form">
            <div class="layui-form-item">
                <label class="layui-form-label">父级分类</label>
                <div class="layui-input-inline" style="width: auto">
                    <select name="pid" id="pid-select" lay-verify="required" lay-filter="pid-select" lay-search>
                        <option value="0" data-level="1">顶级分类</option>
                        <volist name="$treeList" id="vo" empty="暂时没有数据">
                            <option value="{{$vo.id}}" data-level="{{$vo.level}}"
                            <eq name="vo.level" value="3">disabled</eq>
                            <if condition="$vo.id==$current.pid">selected</if>
                            >{{$vo.title}}</option>
                        </volist>
                    </select>
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label">节点名称</label>
                <div class="layui-input-block">
                    <input type="text" name="title" lay-verify="required" lay-reqtext="请输入节点名称" placeholder="请输入节点名称"
                           autocomplete="off" class="layui-input " value="{{$current.title}}">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">节点地址</label>
                <div class="layui-input-block">
                    <input type="text" name="name" lay-verify="required" lay-reqtext="请输入节点地址" placeholder="控制器/方法"
                           autocomplete="off" class="layui-input " value="{{$current.name}}">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">节点图标</label>
                <div class="layui-input-block">
                    <input type="text" style="width: 50%;display:inline-block" name="icon" lay-reqtext="请输入节点图标"
                           placeholder="请输入节点图标如（layui-icon layui-icon-home 或者 iconfont close）" autocomplete="off" class="layui-input " value="{{$current.icon}}">
                    <a onclick="xadmin.open('Iconfont字体图标','http://x.xuebingsi.com/x-admin/v2.2/unicode.html')" class="layui-btn layui-btn-normal layui-btn-radius" href="javascript:;" style="margin-left: 5px">IconFont图标</a>
                    <a onclick="xadmin.open('LayUI字体图标','https://www.layui.com/doc/element/icon.html')" class="layui-btn layui-btn-normal layui-btn-radius" href="javascript:;" style="margin-left: 5px">layui图标</a>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">排序</label>
                <div class="layui-input-inline">
                    <input type="text" name="sorts" lay-verify="number|required" value="100" lay-reqtext="排序必须为数字"
                           placeholder="分类排序" autocomplete="off" class="layui-input " value="{{$current.sorts}}">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">状态</label>
                <div class="layui-input-inline">
                    <input type="radio" name="status" title="显示" value="1" <if condition="$current.status==1">checked</if>/>
                    <input type="radio" name="status" title="隐藏" value="0" <if condition="$current.status==0">checked</if>/>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">是否菜单</label>
                <div class="layui-input-inline">
                    <input type="radio" name="ismenu" title="是" value="1" <if condition="$current.ismenu==1">checked</if>/>
                    <input type="radio" name="ismenu" title="否" value="0" <if condition="$current.ismenu==0">checked</if>checked/>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-input-block">
                    <button class="layui-btn" lay-submit="" lay-filter="add">立即提交</button>
                    <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                </div>
            </div>
            <input type="hidden" name="level" value="0"/>
        </form>
    </div>
</block>
<block name="js">
    <script>
        layui.use(['jquery', 'form', 'layer'], function () {
            var $ = layui.jquery,
                form = layui.form,
                layer = layui.layer;
            //监听提交
            form.on('submit(add)', function (data) {
                let send_json = data.field;
                send_json.level = $("#pid-select option:selected").data('level');
                //发异步，把数据提交给php
                $.ajax({
                    url: '/admin/auth_rule/add',
                    method: 'post',
                    data: send_json,
                    dataType: 'JSON',
                    success: function (res) {
                        if (res.code == 0) {
                            layer.alert("添加成功", {
                                icon: 1
                            }, function () {
                                // 获得frame索引
                                parent.location.reload();
                                var index = parent.layer.getFrameIndex(window.name);
                                //关闭当前frame
                                parent.layer.close(index);
                            });
                        } else {
                            layer.msg(res.msg, {icon: 5});
                        }
                    },
                    error: function (data) {
                        layer.msg('服务器繁忙', {icon: 5});
                    }
                });

                return false;
            });

            //遍历select option
            $(document).ready(function () {
                $("#pid-select option").each(function (text) {
                    var level = $(this).attr('data-level');
                    var text = $(this).text();
                    if (level > 1) {
                        text = "├" + text;
                        for (var i = 1; i <= level; i++) {
                            text = "　　" + text;　//js中连续显示多个空格，需要使用全角的空格
                            //console.log(i+"text:"+text);
                        }
                    }
                    $(this).text(text);

                });

                form.render('select'); //刷新select选择框渲染
            });

        });
    </script>
</block>